<?php
class AuthUser extends Zend_Db_Table
{
	protected $_name ="auth_user";
	protected $_primary = 'id';
	
	public function init()
	{
		$this->config = new Zend_Config_Ini('./application/config/config.ini', null, true);
//		var_dump($this->config);
	}
	
	public function getAllUser()	{
		$User = $this->fetchAll()->toArray();
		return $User;
	}
	
	public function getUserByID($id)	{
		$User = $this->fetchRow('id='.$id)->toArray();
		return $User;
	}
	
	public function getUserNameByID($id)	{
		$User = $this->fetchRow('id='.$id)->toArray();
		$name = $User['username'];
		return $name;
	}
	
	function GetUserRow($username)
	{
		$where = $this->getAdapter()->quoteInto('username=?', $username);
		return $this->fetchRow($where);
	}
	
	function LoginUser($username, $password)
	{
//		$db = $this->getAdapter();
//		$where = $db->quoteInto('username = ?', $username);
//		$userRow = $this->fetchRow($where);
		$userRow = $this->GetUserRow($username);
		var_dump($userRow->toArray());
		echo '<br/><br/>';
		if ($userRow && $userRow->username == $username)
		{
			$passwordSha1 = sha1($password);
//			echo $passwordSha1, '<br/>';
			if ($passwordSha1 == $userRow->password)  # 密码验证通过
			{
				global $USER_INFO;
				$USER_INFO['username'] = $username;
				$USER_INFO['id'] = $userRow->id;
				$passwordCookie = sha1($userRow->password . $this->config->cookie->password);
				$USER_INFO['password'] = $passwordCookie;
				$this->SetCookieUserInfo();
				return $USER_INFO;
			}
		}
		return false;
	}

	function RegUser($username, $password, $petname = null)
	{
		$userRow = $this->GetUserRow($username);
//		echo 'userrow:';
//		var_dump($userRow);
//		echo "<br/><br/>";
		if ($userRow && $userRow->username == $username)
		{
			return false;
		}
		else
		{	
			if (!$petname)
			{
				$petname = $username;
			}
			$nowDate = new Zend_Date();
//			echo $nowDate->get(Zend_Date::W3C), '<br/>';
			$value = Array('username' => $username,
				'petname' => $petname,
				'password' => sha1($password),
				'create_time' => $nowDate->get(Zend_Date::ISO_8601),
				'update_time' => $nowDate->get(Zend_Date::ISO_8601));
			$id = $this->insert($value);
			if ($id)
			{
				global $USER_INFO;
				$value['id'] = $id;
				$passwordSha1 = sha1($password . $this->config->cookie->password);
				$value['password'] = $passwordSha1;
				$USER_INFO = $value;
				$this->SetCookieUserInfo();
				return true;
			}
		}
		return false;
	}
	
	function SetCookieUserInfo()
	{
		global $USER_INFO;
		if ($USER_INFO)
		{
			setcookie($this->config->cookie->name.'[username]', $USER_INFO['username'], time() + $this->config->cookie->expire, '/');
			setcookie($this->config->cookie->name.'[password]', $USER_INFO['password'], time() + $this->config->cookie->expire, '/');
	//		setcookie($this->config->cookie->name.'[id]', $USER_INFO['id'], time() + $this->config->cookie->expire, '/');		
		}
	}
	
	function GetCookieUserInfo()
	{	
//		var_dump($_COOKIE);
//		echo '<br/>';
		if ($_COOKIE && isset($_COOKIE[$this->config->cookie->name]) 
			&& isset($_COOKIE[$this->config->cookie->name]['username'])
			&& isset($_COOKIE[$this->config->cookie->name]['password']))
		{
//			echo $_COOKIE[$this->config->cookie->name]['username'];
			$userRow = $this->getUserRow($_COOKIE[$this->config->cookie->name]['username']);
//			var_dump($userRow);
//			echo '<br/>';
			if ($userRow)
			{
//				echo '<br/><br/>"', $this->config->cookie->password, '" <br/>', strlen($this->config->cookie->password);
				$passwordCookie = sha1($userRow->password . $this->config->cookie->password);
//				echo $passwordCookie, ' == ', $_COOKIE[$this->config->cookie->name]['password'];
				if ($passwordCookie == $_COOKIE[$this->config->cookie->name]['password'])	// 密码验证通过
				{
					global $USER_INFO;
					$USER_INFO['username'] = $_COOKIE[$this->config->cookie->name]['username'];
					$USER_INFO['petname'] = $userRow->petname;
					$USER_INFO['password'] = $_COOKIE[$this->config->cookie->name]['password'];
					$USER_INFO['id'] = $userRow->id;
//					echo 'get cookie user info';
//					var_dump($USER_INFO);
					return $USER_INFO;
				}
			}
		}
		$USER_INFO = Array();
		return $USER_INFO;
	}
	
	function GetUsers($users)
	{
		$where = $this->getAdapter()->quoteInto('username in (?)', array_keys($users));
//		echo '<br/><br/>', $where, '<br/><br/>';
		$fetchUsers = $this->fetchAll($where);
//		var_dump($fetchUsers);
//		echo '<br/><br/>';
		foreach ($fetchUsers as $user)
		{
			$users[$user->username] = $user;
		}
		return $users;
	}
}